package com.bishe.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bishe.config.DataException;
import com.bishe.entity.*;
import com.bishe.entity.response.R;
import com.bishe.service.*;
import com.bishe.util.HttpRequestUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (WzInfo)表控制层
 *
 * @author makejava
 * @since 2024-03-04 00:02:38
 */
@CrossOrigin
@RestController
@RequestMapping("wzInfo")
public class WzInfoController {
    /**
     * 服务对象
     */
    @Resource
    private WzInfoService wzInfoService;

    @Resource
    private FileService fileService;

    @Resource
    private WzTypeService wzTypeService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private UserService userService;


    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param wzInfo 查询实体
     * @return 所有数据
     */
    @GetMapping("/toPage")
    @ApiOperation("分页查询所有数据")
    public R selectAll(Page<WzInfo> page, WzInfo wzInfo) {
        LambdaQueryWrapper<WzInfo> wzInfoLambdaQueryWrapper = new LambdaQueryWrapper<>(wzInfo);
        wzInfoLambdaQueryWrapper.orderByDesc(WzInfo::getCreateTime);

        List<FileInfo> list = fileService.list();
        Map<String, List<FileInfo>> fileMap = list.stream().collect(Collectors.groupingBy(FileInfo::getId));
        Page<WzInfo> wzInfoPage = wzInfoService.page(page, wzInfoLambdaQueryWrapper);

        for (WzInfo wzInfo1 : wzInfoPage.getRecords()) {
            User user = userService.getById(wzInfo1.getCreateBy());
            if (ObjectUtil.isNotNull(user)) {
                wzInfo1.setCreateName(user.getUserName());
            }

            Warehouse byId = warehouseService.getById(wzInfo1.getUid());
            if (ObjectUtil.isNotNull(byId)) {
                wzInfo1.setWarehouseName(byId.getName());
            }

            List<FileInfo> fileInfos = fileMap.get(wzInfo1.getDataId());
            if (CollUtil.isNotEmpty(fileInfos)) {
                wzInfo1.setFileInfo(fileInfos.get(0));
            }

        }

        return R.ok(wzInfoPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    public R selectOne(@PathVariable Serializable id) {
        return R.ok(this.wzInfoService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param wzInfo 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增数据")
    public R insert(@RequestBody WzInfo wzInfo) {
        wzInfo.setCreateBy(StpUtil.getLoginIdAsString());
        wzInfo.setWzRemaining(wzInfo.getWzNum());
        String loginIdAsString = StpUtil.getLoginIdAsString();

        User user = userService.getById(loginIdAsString);
        List<String> types = Arrays.asList("admin", "work");
        if (user != null && types.contains(user.getType())) {
            wzInfo.setStatus(1);
        }
        return R.ok(this.wzInfoService.save(wzInfo));
    }

    /**
     * 修改数据
     *
     * @param wzInfo 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改数据")
    public R update(@RequestBody WzInfo wzInfo) {

        if (wzInfo.getWzNum() - wzInfo.getWzCkNum() < 0) {
            throw new DataException("出库数量不能大于库存数量");
        }
        WzInfo byId = wzInfoService.getById(wzInfo.getId());
        wzInfo.setWzRemaining(byId.getWzRemaining() - wzInfo.getWzCkNum());
        wzInfo.setWzCkNum(byId.getWzCkNum() + wzInfo.getWzCkNum());

        return R.ok(this.wzInfoService.updateById(wzInfo));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("删除数据")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.wzInfoService.removeByIds(idList));
    }
}

